using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GuideSystemUpgrade : MonoBehaviour
{
    public GameObject guidePanel; // 引导界面
    public Text dialogueText; // 文字显示
    public Button nextButton; // "下一步" 按钮
    public float typingSpeed = 0.05f; // 打字机速度

    public string[] dialogues =
    {
        "欢迎来到游戏！",
        "你可以使用WASD移动角色。",
        "按空格键进行跳跃。",
        "祝你游戏愉快！"
    };

    private int currentIndex = 0; // 当前文本索引
    private bool isTyping = false; // 是否正在打字
    private Coroutine typingCoroutine; // 记录当前的打字协程

    private void Start()
    {
        guidePanel.SetActive(false); // 初始隐藏
        nextButton.onClick.AddListener(OnNextButtonClick);
    }

    // 显示引导界面
    public void ShowGuide()
    {
        guidePanel.SetActive(true);
        currentIndex = 0; // 重置索引
        StartTyping(dialogues[currentIndex]);
    }

    private void OnNextButtonClick()
    {
        if (isTyping)
        {
            // 直接显示完整文本，并终止协程
            StopCoroutine(typingCoroutine);
            dialogueText.text = dialogues[currentIndex];
            isTyping = false;
        }
        else
        {
            // 进入下一条对话
            currentIndex++;
            if (currentIndex < dialogues.Length)
            {
                StartTyping(dialogues[currentIndex]);
            }
            else
            {
                guidePanel.SetActive(false); // 关闭引导
            }
        }
    }

    private void StartTyping(string text)
    {
        // 启动打字机协程
        typingCoroutine = StartCoroutine(TypeText(text));
    }

    private IEnumerator TypeText(string text)
    {
        isTyping = true;
        dialogueText.text = "";
        foreach (char letter in text)
        {
            dialogueText.text += letter;
            yield return new WaitForSeconds(typingSpeed);
        }
        isTyping = false;
    }
}
